public class BinarySearchTree {
    public static class TreeNode{
        int val ;
        TreeNode left ;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    TreeNode root = null;
    public TreeNode find(int val){
        if(root == null){
            return null;
        }
        TreeNode cur = root;

        while(cur != null){
            if(cur.val > val){
                cur = cur.left;
            }else if(cur.val < val){
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }

    //自己写的：
    public void insert1(int val){
        if(root == null){
            root = new TreeNode(val);
            return;
        }

        TreeNode cur = root;
        TreeNode parent = null;

        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }
        if(parent.val > val){
            parent.left = new TreeNode(val);
        }else{
            parent.right = new TreeNode(val);
        }
    }

    //insert 修改版本：
    public void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }

        TreeNode cur = root;
        TreeNode parent = null;

        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val){
                parent = cur;
                cur = cur.right;
            }else{
                //不在二叉搜索树中插入两个相同的数
                return;
            }
        }

        //在外面创建好这个要插入的节点
        TreeNode node = new TreeNode(val);
        if(parent.val > val){
            parent.left = node;
        }else {
            parent.right = node;
        }
    }

    //写个中序遍历来检查搜索树插入的正确与否：
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public void remove(int val){
        TreeNode cur = root;
        TreeNode parent = root;
        while (cur != null){
            if (cur.val > val){
                parent = cur;
                cur = cur.left;
            }else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else{
                removeNode(parent,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = root.right;
                return;
            }
            if (parent.left == cur) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = root.left;
                return;
            }
            if (parent.left == cur) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            TreeNode target = cur.right;
            TreeNode targetP = cur;
            while (target.left != null) {
                targetP = target;
                target = target.left;
            }
            cur.val = target.val;
            //如果删除节点的右边总分支中，出现左分支，那么 target 就会在 targetP 的左边
            //如果没有出现左分支，那么 taget 在targetP 的右边
            if (target == targetP.left) {
                targetP.left = target.right;
            } else {
                targetP.right = target.right;
            }

        }
    }
}
